package com.csht.service.Impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.csht.mapper.CshtMessageMapper;
import com.csht.model.CshtMessage;
import com.csht.page.PagerModel;
import com.csht.service.CshtMessageService;
import com.csht.vo.my.CshtMessageVo;

/**
 * 商品留言service
 * @author chenhuiyan
 *
 */
@Service
public class CshtMessageServiceImpl implements CshtMessageService {
	
	@Autowired
	private CshtMessageMapper cshtMessageMapper;

	/**
	 * 根据登录的用户id（被留言者）查询留言
	 */
	@Override
	public PagerModel<CshtMessageVo> getMessageByUserId(Map<String, Integer> res) throws Exception {
		List<CshtMessageVo> cshtMessageVoList = cshtMessageMapper.getMessageByUserId(res);
		Integer totalSize = cshtMessageMapper.getMessageNumberByUserId(res.get("userId"));
		Integer offset = res.get("offset"); //获取偏移量
		Integer messagePageSize = res.get("messagePageSize"); //获取每页的数量
		PagerModel<CshtMessageVo> pagerModel = new PagerModel<CshtMessageVo>();
		pagerModel.setDates(cshtMessageVoList);
		pagerModel.setMessagePageSize(messagePageSize);
		pagerModel.setOffset(offset);
		pagerModel.setTotalSize(totalSize);
		return pagerModel;
	}

	/**
	 * 根据登录的用户id（被留言者）查询留言
	 */
	@Override
	public Integer getMessageNumberByUserId(Integer userId) throws Exception {
		Integer number = cshtMessageMapper.getMessageNumberByUserId(userId);
		return number;
	}

	/**
	 * 根据留言id查询留言
	 */
	@Override
	public CshtMessageVo getMessageByMessageId(Integer messageId) throws Exception {
		CshtMessageVo cshtMessageVo = cshtMessageMapper.getMessageByMessageId(messageId);
		return cshtMessageVo;
	}

	/**
	 * 根据登录的用户id（留言者）查询留言
	 */
	@Override
	public PagerModel<CshtMessageVo> getMessageByMUserId(Map<String, Integer> res) throws Exception {
		List<CshtMessageVo> cshtMessageVoList = cshtMessageMapper.getMessageByMUserId(res);
		Integer totalSize = cshtMessageMapper.getMessageNumberByMUserId(res.get("userId"));
		Integer offset = res.get("offset"); //获取偏移量
		Integer messagePageSize = res.get("messagePageSize"); //获取每页的数量
		PagerModel<CshtMessageVo> pagerModel = new PagerModel<CshtMessageVo>();
		pagerModel.setDates(cshtMessageVoList);
		pagerModel.setMessagePageSize(messagePageSize);
		pagerModel.setOffset(offset);
		pagerModel.setTotalSize(totalSize);
		return pagerModel;
	}

	/**
	 * 根据用户id查询（留言者）留言总数
	 */
	@Override
	public Integer getMessageNumberByMUserId(Integer userId) throws Exception {
		Integer totalSize = cshtMessageMapper.getMessageNumberByMUserId(userId);
		return totalSize;
	}

	/**
	 * 添加留言
	 */
	@Override
	public void insertMessage(CshtMessage cshtMessage) throws Exception {
		cshtMessageMapper.insertMessage(cshtMessage);
	}

}
